package test1.library.wrapper;

import java.io.IOException;
import java.util.Scanner;
import test1.library.command.*;

public class ScannerConsoleWrapper extends ObjectWrapper<Scanner>{

	public ScannerConsoleWrapper() {
		super(new Scanner(System.in, "windows-1251"));
	}

	public void listenAndExecute() throws IOException {
		while (true) {
			if (getObject().hasNext()) {
				String command = getObject().nextLine();
				System.out.println("console: command "+command);
				if (command != null && !command.equals("")) {
					if (executeOrExit(command)){
						break;
					}
				}
			}
		}		
	}

	private boolean executeOrExit(String command) throws IOException {
	    Listbooks list = new Listbooks ();
            addbooks add = new addbooks ();
            search search = new search ();
            delete delete = new delete ();
            addinfo addinfo = new addinfo ();
            if (command.equals("Выход")) {
			getObject().close();
			return true;
		} else if (command.equals("Добавить")) {
			add.addbook();
		}
                else if (command.equals ("Удаление")) {
                delete.delete();
                }
            else if (command.equals ("Поиск")) {
                search.search();
                }
            else if (command.equals ("Список")) {
                list.listbooks();
                }
             else if (command.equals ("Информация")) {
                addinfo.addinfo();
               
}
            else  {
                System.out.println("Неизвестная команда");
                }
           
		return false;
	}

	public Scanner getScanner() {
		return getObject();
	}

	public void close() {
		getScanner().close();
	}
}
